package com.goresor.controller.security;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.goresor.service.IUserService;

@Controller
public class OpenIdController {

	public static final Logger log = LoggerFactory.getLogger(OpenIdController.class);

	private static final String USER_OPENID_EMAIL = "USER_OPENID_EMAIL";
	private static final String USER_OPENID_USERID = "USER_OPENID_USERID";
	
	@Autowired
	private IUserService userService;

	@RequestMapping(method = RequestMethod.GET, value = "/registrationOpenid")
	public String registrationOpenId(HttpServletRequest request) {
		String email = (String) request.getSession().getAttribute(USER_OPENID_EMAIL);
		String userId = (String) request.getSession().getAttribute(USER_OPENID_USERID);
		
		if (email != null) {
			userService.createUser(userId, "unused", email);
			log.info("Your account has been created. Please log using your OpenID.");
			return null;
		} else {
			log.info("Please register using your OpenID.");
			return null;
		}
	}
	
}
